package pub.tbc.toolkit.web.config.rest.controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pub.tbc.toolkit.exception.GlobalException;
import pub.tbc.toolkit.web.config.rest.service.ConfigService;

/**
 * @author tbc tianbencai@e-eduspace.com
 * @version 1.0 {2016年5月18日 下午2:11:07}
 */
@Controller
@RequestMapping("config")
public class ConfigController {
    @SuppressWarnings("unused")
    private Logger log = LoggerFactory.getLogger(ConfigController.class);

    @Autowired
    private ConfigService service;

    @ResponseBody
    @RequestMapping(method = {RequestMethod.GET})
    public Map<String, Set<String>> fileList() throws GlobalException {
        return service.fileList();
    }

    @ResponseBody
    @RequestMapping(value = "/filekeys/{file}", method = {RequestMethod.GET})
    public Map<String, List<String>> propKeysByFile(@PathVariable String file) throws GlobalException {
        return service.propKeysByFile(file);
    }

    @ResponseBody
    @RequestMapping(value = "/file/{file}", method = {RequestMethod.GET})
    public Map<String, Map<String, String>> propsByFile(@PathVariable String file) throws GlobalException {
        return service.propsByFile(file);
    }

    @ResponseBody
    @RequestMapping(value = "refresh", method = {RequestMethod.PUT})
    public Map<String, String> refresh() throws GlobalException {
        return service.refresh();
    }

    @ResponseBody
    @RequestMapping(value = "/{configFile}/{propName}/{value}", method = {RequestMethod.PUT})
    public Map<String, String> update(
            @PathVariable String configFile,
            @PathVariable String propName,
            @PathVariable String value) throws GlobalException {
        Objects.requireNonNull(configFile);
        Objects.requireNonNull(propName);
        Objects.requireNonNull(value);

        return service.update(configFile, propName, value);
    }

}
